/**
 * 用户配置类
 * @author hejiacheng
 * @date 2021-12-3
 */

import storageUtil from '@/utils/storageUtil.js'


let userConfig = {}

let env = null



/**
 * 获取环境对象
 * @param {时间对象} date 
 */
function getProvider(data) {
	return new Promise(function(resolve, reject) {
		let env = storageUtil.getEnv()
		if(env){
			resolve && resolve(env)
			return
		}
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				console.log('getEnv success : ', res)
				if (res && res.provider && res.provider.length > 0) {
					storageUtil.setEnv(res.provider[0])
					resolve && resolve(res.provider[0])
				} else {
					reject && reject(res)
				}

			},
			fail: function(res) {
				reject && reject(res)
			}
		});
	})
}

/**
 * 获取环境对象
 * @param {时间对象} date 
 */
function getUserConfig(data) {
	return new Promise(function(resolve, reject) {
		getProvider().then((env) => {
			// 头条
			if (env == 'toutiao') {
				resolve && resolve({
					appid: 'tt546a22d7cb54952401', //小程序的AppID
				})
			}else if (env == 'qq') {
				resolve && resolve({
					appid: '1112136237', //小程序的AppID
					secret: 'c0eeT62R2J6L8dMT' //小程序的AppSecret
				})
			}else if (env == 'weixin') {
				resolve && resolve({
					appid: 'wx4c984dfbee8b17cb', //小程序的AppID
				})
			}
		}, (res) => {
			console.log('无法查询到当前的环境....')
			reject && reject()
		})
	})
}

export {
	getUserConfig,
	getProvider
}
